<?php
/**
* Zend_Db_Adapter interface for Shuffler_Mapper
* 
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* 
* @copyright Copyright (c) 2008-2009 Ne8, LLC <josh.ribakoff@gmail.com>
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) 
* @license New BSD License
* @version 0.1
* @category Shuffler
* @package Shuffler_Mapper
* @subpackage Adapter
*/
class Shuffler_Mapper_Adapter_ZendDb implements Shuffler_Mapper_Adapter
{
    protected $db;
    
    /**
    * Attempts to locate adapter from Zend_Registry, pass adapter to override
    *
    * @param Zend_Db_Adapter adapter to set
    */
    public function __construct( $db = NULL )
    {
        $this->db = isset( $db ) ? $db : Zend_Registry::get( 'db' ); 
    }
    
    /**
     * Returns the column descriptions for a table.
     *
     * The return value is an associative array keyed by the column name,
     * as returned by the RDBMS.
     *
     * The value of each array element is an associative array
     * with the following keys:
     *
     * SCHEMA_NAME => string; name of database or schema
     * TABLE_NAME  => string;
     * COLUMN_NAME => string; column name
     * COLUMN_POSITION => number; ordinal position of column in table
     * DATA_TYPE   => string; SQL datatype name of column
     * DEFAULT     => string; default expression of column, null if none
     * NULLABLE    => boolean; true if column can have nulls
     * LENGTH      => number; length of CHAR/VARCHAR
     * SCALE       => number; scale of NUMERIC/DECIMAL
     * PRECISION   => number; precision of NUMERIC/DECIMAL
     * UNSIGNED    => boolean; unsigned property of an integer type
     * PRIMARY     => boolean; true if column is part of the primary key
     * PRIMARY_POSITION => integer; position of column in primary key
     *
     * @param string $tableName
     * @param string $schemaName OPTIONAL
     * @return array
     */
    public function describeTable( $tableName, $schemaName = null )
    {
        return $this->db->describeTable( $tableName, $schemaName );
    }
    
    /**
    * Creates and returns a new Zend_Db_Select object for this adapter.
    *
    * @return Zend_Db_Select
    */
    public function select()
    {
        return $this->db->select();
    }
    
    /**
    * Prepares and executes an SQL statement with bound data.
    *
    * @param  mixed  $sql  The SQL statement with placeholders.
    *                      May be a string or Zend_Db_Select.
    * @param  mixed  $bind An array of data to bind to the placeholders.
    * @return Zend_Db_Statement_Interface
    */
    public function query( $sql, $bind = array() )
    {
        return $this->db->query( $sql, $bind );
    }
    
    /**
    * Finds rows where table field name matches given value
    * 
    * @param string Name of field
    * @param value to match
    * @return collection of Shuffler_Model
    */
    public function findByField( $fieldName, $value )
    {
        $db = $this->getReadAdapter();
        $where = $db->quoteInto( $db->quoteIdentifier( $fieldName ) . ' = ?', $value );
        return $this->findWhere( $where );
    }
    
    /**
     * Quotes a value and places into a piece of text at a placeholder.
     *
     * The placeholder is a question-mark; all placeholders will be replaced
     * with the quoted value.   For example:
     *
     * <code>
     * $text = "WHERE date < ?";
     * $date = "2005-01-02";
     * $safe = $sql->quoteInto($text, $date);
     * // $safe = "WHERE date < '2005-01-02'"
     * </code>
     *
     * @param string  $text  The text with a placeholder.
     * @param mixed   $value The value to quote.
     * @param string  $type  OPTIONAL SQL datatype
     * @param integer $count OPTIONAL count of placeholders to replace
     * @return string An SQL-safe quoted value placed into the orignal text.
     */
    public function quoteInto($text, $value, $type = null, $count = null)
    {
        return $this->db->quoteInto( $text, $value, $type, $count );
    }
    
    /**
     * Quotes an identifier.
     *
     * Accepts a string representing a qualified indentifier. For Example:
     * <code>
     * $adapter->quoteIdentifier('myschema.mytable')
     * </code>
     * Returns: "myschema"."mytable"
     *
     * Or, an array of one or more identifiers that may form a qualified identifier:
     * <code>
     * $adapter->quoteIdentifier(array('myschema','my.table'))
     * </code>
     * Returns: "myschema"."my.table"
     *
     * The actual quote character surrounding the identifiers may vary depending on
     * the adapter.
     *
     * @param string|array|Zend_Db_Expr $ident The identifier.
     * @param boolean $auto If true, heed the AUTO_QUOTE_IDENTIFIERS config option.
     * @return string The quoted identifier.
     */
    public function quoteIdentifier($ident, $auto=false)
    {
        return $this->db->quoteIdentifier( $ident, $auto );
    }
    
    /**
     * Updates table rows with specified data based on a WHERE clause.
     *
     * @param  mixed        $table The table to update.
     * @param  array        $bind  Column-value pairs.
     * @param  mixed        $where UPDATE WHERE clause(s).
     * @return int          The number of affected rows.
     */
    public function update($table, array $bind, $where = '')
    {
        return $this->db->update( $table, $bind, $where );
    }
    
    /**
     * Deletes table rows based on a WHERE clause.
     *
     * @param  mixed        $table The table to update.
     * @param  mixed        $where DELETE WHERE clause(s).
     * @return int          The number of affected rows.
     */
    public function delete($table, $where = '')
    {
        return $this->db->delete( $table, $where );
    }
    
    /**
     * Inserts a table row with specified data.
     *
     * @param mixed $table The table to insert data into.
     * @param array $bind Column-value pairs.
     * @return int The number of affected rows.
     */
    public function insert($table, array $bind)
    {
        return $this->db->insert( $table, $bind );
    }
    
    public function lastInsertId()
    {
        return $this->db->lastInsertId();
    }
    
    /**
     * Leave autocommit mode and begin a transaction.
     *
     * @return bool True
     */
    public function beginTransaction()
    {
        return $this->db->beginTransaction();
    }
    
    /**
     * Commit last opened transaction
     *
     * @return bool True
     */
    public function commit()
    {
        return $this->db->commit();
    }
    
    /**
     * Roll back a transaction and return to autocommit mode.
     *
     * @return bool True
     */
    public function rollBack()
    {
        return $this->db->rollBack();
    }
    
    
}